<?php
/**
 * Created by PhpStorm.
 * User: rocky
 * Date: 2019-07-14
 * Time: 10:27
 */

namespace app\api\controller\v1;
use app\api\service\WechatMiniService;
use app\api\service\WechatService;
use app\common\controller\BaseApiController;
use app\common\facade\Token;
use app\common\model\ShopAgent;
use app\common\tools\Files;
use app\common\model\SystemUser;
use think\Db;

class Common extends BaseApiController
{
    //小程序code授权换取令牌
    public function login(){
        $code = $this->request->param('code');
        $res = WechatMiniService::login($code);
        if($res){
            $this->successCode($res);
        }else{
            $this->errorCode(1001);
        }
    }
    //上传文件
    public function upload(){
        $file = new Files();
        $data = $file->upload();
        $this->successCode($data);
    }
    //获取微信code跳转链接
    public function wxGetCodeUrl()
    {
        $url = $this->request->param('url');
        $data = WechatService::getCodeTargetUrl($url, 1);
        $this->successCode($data);
    }

    //微信公众号code登录获取token
    public function wxCodeLogin()
    {
        $code = $this->request->param('code');
        $res = WechatService::codeLogin($code);
        if ($res) {
            $this->successCode($res);
        } else {
            $this->errorCode(1001);
        }
    }

    public function agentLogin()
    {
        $account = $this->request->param('account');
        $password = $this->request->param('password');
        $user=ShopAgent::where('phone',$account)->where('password',md5($password))->find();
        if ($user){
            $res = Token::encode($user->getData());
            $this->successCode($res);
        }else{
            $this->errorCode(100,'手机号或密码错误');
        }
    }

    //微信jsdk
    public function wxJsdk()
    {
        $url = $this->request->post('url');
        $data = WechatService::getWebJssdkSign($url);
        $this->successCode($data);
    }
    
    //上传图片
    public function uploadImg()
    {
        //获取文件对象
        $file = $this->request->file('file');
        //验证并上传
        $info = $file->validate(['size'=>'5242880','ext'=>'jpg,gif,png'])
            ->move('upload');
        //判断是否成功
        if($info){
            $data['src'] = 'upload/'.$info->getSaveName();
            $this->successCode($data);
        }else{
            $this->errorCode(100,'上传失败');
        }
    }
    
    // 商家端登录
     public function storeLogin(){
        $phone = $this->request->post('phone');
        $password = md5($this->request->post('password'));
        
        $map = "username='{$phone}' OR phone='{$phone}'";
        $user = SystemUser::where('is_deleted','0')->where($map)->with('store')->find();
        
        if($password == $user['password']){
            $tokenArr = Token::encode($user);
            $data = [
                'tokenArr' =>$tokenArr,
                'user' => $user
            ];
            if($tokenArr){
                $this->successCode($data);
            }else{
                $this->errorCode(1001);
            }
        }else{
             $this->errorCode(100,'账户或密码错误');  
        }
     }
}